/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gebze.Util;

import com.asprise.util.jtwain.JTwainException;
import com.asprise.util.jtwain.Source;
import com.asprise.util.jtwain.SourceManager;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 * Tarama işlemini yapan class
 *
 * @author Özcan ÇAĞIRICI <ozcan.cagirici@gmail.com>
 *
 */
public class Scanner {

   private Source source = null;

    public Scanner() throws JTwainException {
        try {
          source = SourceManager.instance().getDefaultSource();
        } catch (JTwainException ex) {
            System.out.println("Bagli tarayici bulunamadi.");
        }
    }

    /**
     * Tarama işlemini yapar ve datayı byte[] olarak getirir.
     *
     * @return
     */
    public byte[] getImage() throws Exception {
        BufferedImage image = null;
        byte[] imageInBytes;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            source.open();
            System.out.println("cihaz acildi: " + source);
            image = source.acquireImageAsBufferedImage();
        } catch (JTwainException ex) {
            System.out.println("Cihaz hatasi: " + ex);
        }
        try {
            ImageIO.write(image, "jpg", bos);
        } catch (IOException ex) {
            System.out.println("Image olusturulamadi" + ex);
        }
        imageInBytes = bos.toByteArray();
        return imageInBytes;
    }
}